<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of iGet
 *
 * @author mateusz.ledzewicz
 */
abstract class AbstractGet {
    
    const date                      = 'date';
    const openPrice                 = 'openPrice';
    const highPrice                 = 'highPrice';
    const lowPrice                  = 'lowPrice';
    const closePrice                = 'closePrice';
    const change                    = 'change';
    const volume                    = 'volume'; 
    const transactionNumber         = 'transactionNumber';
    const value                     = 'value';
    
    
    private static $csvSource       = 'out';
    private static $csvDelimeter    = ',';
    private static $csvEndLine      = "\n";
    public static $mapper;
    
    protected $isin;
    protected $dataFrom;
    protected $dataTo;
    protected $data;
    protected $dataArray;
    
    public function __construct($config, $csvDelimeter=', ') {
        if($csvDelimeter)
            self::$csvDelimeter = $csvDelimeter;
        self::$mapper = null;
        if(is_null(self::$mapper))
        {
            $this->createMapper();
            
        }
        $this->isin     = $config['isin'];
        $this->dataFrom = $config['dateFrom'];
        $this->dataTo   = $config['dateTo'];
        
        $this->init($config['dateFrom'], $config['dateTo'], $config['instrumentType'], $config['isin']);
        $this->prepareArray();
    }
    
    abstract protected function init($dateFrom, $dateTo, $instrumentType, $isin);
    abstract protected function createMapper();
    
    protected function prepareArray(){
        if($this->dataArray){
            throw new Exception('Data is prepared');
        }
        $lines = explode(self::$csvEndLine, $this->data);
        foreach($lines as $line){
                $this->dataArray[] = str_getcsv($line, self::$csvDelimeter);
        }
        
        if(is_null($this->dataArray[count($this->dataArray)-1][0]))
            array_pop($this->dataArray);
        
        
    }
    
    public function cutData($config){
        $data = Array();
        foreach($this->dataArray as $row => $line)
        {
            $data[$row] = array();
            foreach($config as $column)
            {
                $column = self::$mapper[$column];
                if(!isSet($line[$column]))
                {
                    unset($data[$row]);
                    break;
                }
                $data[$row][] = $line[$column];
            }
        }
        $this->dataArray = $data;
    }    
    
    public function getArray(){
        return $this->dataArray;
    }
    
    public function saveCsv($name=null, $source=null, $copyNumber = 1){
        if(is_null($name))
        {
            $name = 'output_'.$this->isin.'_'.$this->dataFrom.'_'.$this->dataTo;
        }
        $baseName = $name;
        $name .= '_copy'.$copyNumber;
        
        if(is_null($source))
        {
            $source = self::$csvSource;
        }
        $source = rtrim($source, '/');
        if(file_exists($source.'/'.$name.'.csv'))
        {
            return $this->saveCsv($baseName, $source, $copyNumber+1);
        }
        
        if(!$handleToCsv = fopen($source.'/'.$name.'.csv', "a")){
            Throw new Exception('Problem with file');
        }
        foreach($this->dataArray as $line){
            fputcsv($handleToCsv, $line, self::$csvDelimeter);
        }
        return $source.'/'.$name.'.csv';
        fclose($handleToCsv);
    }
    
    public function removeHeader(){
        unset($this->dataArray[0]);
    }
    
    public function cutFromPost($post)
    {
        $cutElements = array();
        $c = get_class( $this );
        unset($post['dateFrom']);
        unset($post['dateTo']);
        unset($post['isin']);
        unset($post['instrumentType']);
        unset($post['type']);
        foreach($post as $key => $value)
        {           
            $cutElement = constant($c.'::'. $key);
            if($value == 'on' && !is_null($cutElement)) $cutElements[] = $cutElement; 
        }
        if(count($cutElements) > 0)
            $this->cutData ($cutElements);
    }
    
    
}

?>
